<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Keping_segmen_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->CI = get_instance();
    }

    function insert($data) {
        $this->db->insert('keping_jalan_segmen', $data);
    }

    function get_santri() {
        $this->db->select("*");
        $this->db->from('santri');
        $query = $this->db->get();
        return $query;
    }

    //list flexy
    function get_data($no_ruas, $id_periode) {
        //Select table name
        //$table_name = "kelas_santri";

        //Build contents query
		$this->db->select('*');
		$this->db->from('keping_jalan_segmen');
		//$this->db->join('santri as s', 'ks.id_santri = s.id_santri', 'left');
		$this->db->where('NO_RUAS', $no_ruas);
		$this->db->where('ID_PERIODE', $id_periode);
		$this->CI->flexigrid->build_query();
		
		

        //Get contents
        $return['records'] = $this->db->get();

        //Build count query
		$this->db->select('*');
		$this->db->from('keping_jalan_segmen');
		//$this->db->join('santri as s', 'ks.id_santri = s.id_santri', 'left');
		$this->db->where('NO_RUAS', $no_ruas);
		$this->db->where('ID_PERIODE', $id_periode);
		$this->CI->flexigrid->build_query(FALSE);

        $return['record_count'] = $this->db->count_all_results();

        //Return all
        return $return;
    }

	
	function get_data_keping($id_periode, $no_ruas){
		$this->db->select('*');
		$this->db->from('keping_jalan_segmen');
		$this->db->where('ID_PERIODE', $id_periode);
		$this->db->where('NO_RUAS', $no_ruas);
		$this->db->order_by('KEPING', 'asc');
		$query = $this->db->get();
        return $query;
	}
	
	
    function delete($id) {
        $this->db->where('ID_KEPING', $id);
        $this->db->delete('keping_jalan');
    }
	
	
	function selectall() {
        $this->db->select("*");
        $this->db->from('keping_jalan_segmen');
        $query = $this->db->get();
        return $query;
    }
	
	
	//------------sampah
	
	function delete_by_kelas($id_kelas) {
        $this->db->where('id_kelas', $id_kelas);
        $this->db->delete('kelas_santri');
    }

    function selectone($id) {
        $this->db->select("*");
        $this->db->from('keping_jalan');
        $this->db->where('ID_KEPING', $id);
        $query = $this->db->get();
        return $query;
    }

    function update($id, $data) {
        $this->db->where('ID_KEPING', $id);
        $this->db->update('keping_jalan', $data);
    }
	
	
	function data_form_nilai($id_kelas){
		$this->db->select('s.*, ks.*');
		$this->db->from('kelas_santri ks');
		$this->db->join('santri as s', 'ks.id_santri = s.id_santri', 'left');
		$this->db->where('ks.id_kelas', $id_kelas);
		$this->db->order_by('s.munakosah', 'asc');
		$query = $this->db->get();
        return $query;
	}

	//---------kelas by santri----------------
	
    function get_data_kelas($id_santri) {
        
        //Build contents query
		$this->db->select('K.*, KS.*, P.*, U.*');
		$this->db->from('kelas_santri AS KS');
		$this->db->join('kelas AS K', 'KS.id_kelas = K.ID_kelas', 'left');
		$this->db->join('periode AS P', 'K.ID_periode = P.ID_periode', 'left');
		$this->db->join('ustad AS U', 'K.ID_USTAD = U.id_ustad', 'left');
		$this->db->where('KS.id_santri', $id_santri);
		$this->CI->flexigrid->build_query();
		
        //Get contents
        $return['records'] = $this->db->get();

        //Build count query
		$this->db->select('K.*, KS.*, P.*');
		$this->db->from('kelas_santri AS KS');
		$this->db->join('kelas AS K', 'KS.id_kelas = K.ID_kelas', 'left');
		$this->db->join('periode AS P', 'K.ID_periode = P.ID_periode', 'left');
		$this->db->where('KS.id_santri', $id_santri);
		$this->CI->flexigrid->build_query(FALSE);

        $return['record_count'] = $this->db->count_all_results();

        //Return all
        return $return;
    }

	function get_all_kelas() {
		$this->db->select('K.*, P.*, U.*');
		$this->db->from('kelas AS K');
		$this->db->join('periode AS P','K.ID_periode = P.ID_periode','left');		
		$this->db->join('ustad AS U','K.ID_USTAD = U.id_ustad','left');		
		$this->CI->flexigrid->build_query();

        //Get contents
        $return['records'] = $this->db->get();
		
		//Build count query
        $this->db->select('K.*, P.*, U.*');
		$this->db->from('kelas AS K');
		$this->db->join('periode AS P','K.ID_periode = P.ID_periode','left');		
		$this->db->join('ustad AS U','K.ID_USTAD = U.id_ustad','left');
		$this->CI->flexigrid->build_query(FALSE);

        $return['record_count'] = $this->db->count_all_results();

        //Return all
        return $return;
		
	}
	
	
	
	function get_kelas_per_periode($id_periode) {
		$this->db->select('K.*, P.*, U.*');
		$this->db->from('kelas AS K');
		$this->db->join('periode AS P','K.ID_periode = P.ID_periode','left');		
		$this->db->join('ustad AS U','K.ID_USTAD = U.id_ustad','left');		
		$this->db->where('K.ID_periode', $id_periode);
		$this->CI->flexigrid->build_query();

        //Get contents
        $return['records'] = $this->db->get();
		
		//Build count query
        $this->db->select('K.*, P.*, U.*');
		$this->db->from('kelas AS K');
		$this->db->join('periode AS P','K.ID_periode = P.ID_periode','left');		
		$this->db->join('ustad AS U','K.ID_USTAD = U.id_ustad','left');
		$this->db->where('K.ID_periode', $id_periode);
		$this->CI->flexigrid->build_query(FALSE);

        $return['record_count'] = $this->db->count_all_results();

        //Return all
        return $return;
		
	}
		
	function ekspor_data_kelas($id_santri) {   
        //Build contents query
		$this->db->select('K.*, KS.*, P.*, S.*');
		$this->db->from('kelas_santri AS KS');
		$this->db->join('kelas AS K', 'KS.id_kelas = K.ID_kelas', 'left');
		$this->db->join('periode AS P', 'K.ID_periode = P.ID_periode', 'left');
		$this->db->join('santri AS S', 'KS.id_santri = S.id_santri', 'left');
		$this->db->where('KS.id_santri', $id_santri);
		$this->db->order_by('NAMA_periode','desc');
		$getData = $this->db->get();
		if($getData->num_rows() > 0)
		return $getData->result_array();
		else
		return null;
			
	}
	
	function save_nilai($id_santri, $id_kelas, $data) {
        $this->db->where('id_santri', $id_santri);
        $this->db->where('id_kelas', $id_kelas);
        $this->db->update('kelas_santri', $data);
    }
	
	
	
	
	function get_data_ustad($id_kelas){
		$this->db->select('*');
		$this->db->from('kelas k');
		$this->db->join('ustad u', 'k.id_ustad = u.id_ustad', 'left');
		$this->db->where('k.ID_kelas', $id_kelas);
		$query = $this->db->get();
        return $query;
	}	
	
}

?>